/*     */ package com.zimbra.cs.mailclient.imap;
/*     */ 
/*     */ import java.io.IOException;
/*     */ import java.util.AbstractSet;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class Flags
/*     */   extends AbstractSet<Atom>
/*     */ {
/*     */   private int flagMask;
/*     */   private Set<Atom> otherFlags;
/*     */   private static final int MASK_ANSWERED = 1;
/*     */   private static final int MASK_FLAGGED = 2;
/*     */   private static final int MASK_DELETED = 4;
/*     */   private static final int MASK_SEEN = 8;
/*     */   private static final int MASK_DRAFT = 16;
/*     */   private static final int MASK_RECENT = 32;
/*     */   private static final int MASK_STAR = 64;
/*     */   private static final int MASK_NOSELECT = 128;
/*     */   private static final int MASK_MARKED = 256;
/*     */   private static final int MASK_UNMARKED = 512;
/*     */   private static final int MASK_NOINFERIORS = 1024;
/*  48 */   private static final CAtom[] FLAG_ATOMS = { CAtom.F_ANSWERED, CAtom.F_FLAGGED, CAtom.F_DELETED, CAtom.F_SEEN, CAtom.F_DRAFT, CAtom.F_RECENT, CAtom.F_STAR, CAtom.F_NOSELECT, CAtom.F_MARKED, CAtom.F_UNMARKED, CAtom.F_NOINFERIORS };
/*     */   
/*     */ 
/*     */ 
/*     */   public static Flags read(ImapInputStream is)
/*     */     throws IOException
/*     */   {
/*  55 */     Flags flags = new Flags();
/*  56 */     is.skipSpaces();
/*  57 */     is.skipChar('(');
/*  58 */     is.skipSpaces();
/*  59 */     while (!is.match(')')) {
/*  60 */       flags.set(is.readFlag());
/*  61 */       is.skipSpaces();
/*     */     }
/*  63 */     return flags;
/*     */   }
/*     */   
/*     */   public static Flags fromSpec(String spec) throws IOException {
/*  67 */     Flags flags = new Flags();
/*  68 */     for (int i = 0; i < spec.length(); i++) {
/*  69 */       flags.set(getMask(spec.charAt(i)));
/*     */     }
/*  71 */     return flags;
/*     */   }
/*     */   
/*     */   private static int getMask(char c) {
/*  75 */     switch (c) {
/*  76 */     case 'a':  return 1;
/*  77 */     case 'f':  return 2;
/*  78 */     case 'd':  return 4;
/*  79 */     case 's':  return 8;
/*  80 */     case 'r':  return 32;
/*  81 */     case 't':  return 64;
/*  82 */     case 'x':  return 16;
/*     */     }
/*  84 */     throw new IllegalArgumentException("Invalid flag spec char '" + c + "'");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  91 */   public void setAnswered() { set(1); }
/*  92 */   public void setFlagged() { set(2); }
/*  93 */   public void setDeleted() { set(4); }
/*  94 */   public void setSeen() { set(8); }
/*  95 */   public void setDraft() { set(16); }
/*  96 */   public void setRecent() { set(32); }
/*     */   
/*     */   public void set(String flag) {
/*  99 */     set(new Atom(flag));
/*     */   }
/*     */   
/*     */   public void set(Atom flag) {
/* 103 */     add(flag);
/*     */   }
/*     */   
/*     */   public boolean add(Atom flag)
/*     */   {
/* 108 */     int mask = getMask(CAtom.get(flag));
/* 109 */     if (mask != 0) {
/* 110 */       boolean b = isSet(mask);
/* 111 */       set(mask);
/* 112 */       return b;
/*     */     }
/* 114 */     if (this.otherFlags == null) {
/* 115 */       this.otherFlags = new HashSet();
/*     */     }
/* 117 */     return this.otherFlags.add(flag);
/*     */   }
/*     */   
/* 120 */   public void unsetAnswered() { unset(1); }
/* 121 */   public void unsetFlagged() { unset(2); }
/* 122 */   public void unsetDeleted() { unset(4); }
/* 123 */   public void unsetSeen() { unset(8); }
/* 124 */   public void unsetDraft() { unset(16); }
/* 125 */   public void unsetRecent() { unset(32); }
/*     */   
/*     */   public void unset(String flag) {
/* 128 */     unset(new Atom(flag));
/*     */   }
/*     */   
/*     */   public void unset(Atom flag) {
/* 132 */     remove(flag);
/*     */   }
/*     */   
/*     */   public boolean remove(Object obj)
/*     */   {
/* 137 */     if (!(obj instanceof Atom)) return false;
/* 138 */     Atom flag = (Atom)obj;
/* 139 */     int mask = getMask(CAtom.get(flag));
/* 140 */     if (mask != 0) {
/* 141 */       boolean b = !isSet(mask);
/* 142 */       unset(mask);
/* 143 */       return b;
/*     */     }
/* 145 */     return this.otherFlags.remove(flag);
/*     */   }
/*     */   
/* 148 */   public boolean isAnswered() { return isSet(1); }
/* 149 */   public boolean isFlagged() { return isSet(2); }
/* 150 */   public boolean isDeleted() { return isSet(4); }
/* 151 */   public boolean isSeen() { return isSet(8); }
/* 152 */   public boolean isDraft() { return isSet(16); }
/* 153 */   public boolean isRecent() { return isSet(32); }
/* 154 */   public boolean isStar() { return isSet(64); }
/* 155 */   public boolean isNoselect() { return isSet(128); }
/* 156 */   public boolean isMarked() { return isSet(256); }
/* 157 */   public boolean isUnmarked() { return isSet(512); }
/* 158 */   public boolean isNoinferiors() { return isSet(1024); }
/*     */   
/*     */   public boolean isSet(String flag) {
/* 161 */     return isSet(new Atom(flag));
/*     */   }
/*     */   
/*     */   public boolean isSet(Atom flag) {
/* 165 */     return contains(flag);
/*     */   }
/*     */   
/*     */   public boolean contains(Object obj)
/*     */   {
/* 170 */     if (!(obj instanceof Atom)) return false;
/* 171 */     Atom flag = (Atom)obj;
/* 172 */     int mask = getMask(CAtom.get(flag));
/* 173 */     return (this.otherFlags != null) && (this.otherFlags.contains(flag)) ? true : mask != 0 ? isSet(mask) : false;
/*     */   }
/*     */   
/*     */ 
/*     */   public int size()
/*     */   {
/* 179 */     int size = this.otherFlags != null ? this.otherFlags.size() : 0;
/* 180 */     return size + bitcount(this.flagMask);
/*     */   }
/*     */   
/*     */   public Iterator<Atom> iterator()
/*     */   {
/* 185 */     List<Atom> flags = new ArrayList(size());
/* 186 */     for (CAtom catom : FLAG_ATOMS) {
/* 187 */       if (isSet(getMask(catom))) {
/* 188 */         flags.add(catom.atom());
/*     */       }
/*     */     }
/* 191 */     if (this.otherFlags != null) {
/* 192 */       flags.addAll(this.otherFlags);
/*     */     }
/* 194 */     assert (flags.size() == size());
/* 195 */     return flags.iterator();
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 200 */     StringBuilder sb = new StringBuilder("(");
/* 201 */     Iterator<Atom> it = iterator();
/* 202 */     if (it.hasNext()) {
/* 203 */       sb.append(it.next());
/* 204 */       while (it.hasNext()) {
/* 205 */         sb.append(' ').append(it.next());
/*     */       }
/*     */     }
/* 208 */     return ')';
/*     */   }
/*     */   
/*     */   public void write(ImapOutputStream os) throws IOException {
/* 212 */     os.write(40);
/* 213 */     Iterator<Atom> it = iterator();
/* 214 */     if (it.hasNext()) {
/* 215 */       ((Atom)it.next()).write(os);
/* 216 */       while (it.hasNext()) {
/* 217 */         os.write(32);
/* 218 */         ((Atom)it.next()).write(os);
/*     */       }
/*     */     }
/* 221 */     os.write(41);
/*     */   }
/*     */   
/*     */   private void set(int mask) {
/* 225 */     this.flagMask |= mask;
/*     */   }
/*     */   
/*     */   private void unset(int mask) {
/* 229 */     this.flagMask &= (mask ^ 0xFFFFFFFF);
/*     */   }
/*     */   
/*     */   private boolean isSet(int mask) {
/* 233 */     return (this.flagMask & mask) != 0;
/*     */   }
/*     */   
/*     */   private int getMask(CAtom catom) {
/* 237 */     switch (catom) {
/*     */     case F_ANSWERED: 
/* 239 */       return 1;
/*     */     case F_FLAGGED: 
/* 241 */       return 2;
/*     */     case F_DELETED: 
/* 243 */       return 4;
/*     */     case F_SEEN: 
/* 245 */       return 8;
/*     */     case F_DRAFT: 
/* 247 */       return 16;
/*     */     case F_RECENT: 
/* 249 */       return 32;
/*     */     case F_STAR: 
/* 251 */       return 64;
/*     */     case F_NOSELECT: 
/* 253 */       return 128;
/*     */     }
/* 255 */     return 0;
/*     */   }
/*     */   
/*     */ 
/*     */   private static int bitcount(int n)
/*     */   {
/* 261 */     long tmp = n & 0xFFFFFFFF;
/* 262 */     tmp = tmp - (tmp >> 1 & 0xDB6DB6DB) - (tmp >> 2 & 0x49249249);
/* 263 */     return (int)((tmp + (tmp >> 3) & 0xC71C71C7) % 63L);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/imap/Flags.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */